package com.example.ktstoragedemo

import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class ImageAdapter(private val context:Context,private val imageList:ArrayList<Uri>,private val imageSize: Int)
    :RecyclerView.Adapter<ImageAdapter.ViewHolder>() {
    class ViewHolder(view:View):RecyclerView.ViewHolder(view){
        val img_image:ImageView=view.findViewById(R.id.img_image)
        val tv_name:TextView=view.findViewById(R.id.tv_name)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageAdapter.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.image_item_layout,parent,false)
        val holder = ViewHolder(view)
        holder.img_image.setOnClickListener {
            val position = holder.adapterPosition
            Toast.makeText(context,"$position",Toast.LENGTH_SHORT).show()
        }
        return holder
    }

    override fun onBindViewHolder(holder: ImageAdapter.ViewHolder, position: Int) {
        holder.img_image.layoutParams.width = imageSize
        holder.img_image.layoutParams.height = imageSize
        val image = imageList[position]
        holder.tv_name.text="$position"
        Glide.with(context).load(image).into(holder.img_image)
    }

    override fun getItemCount(): Int {
        return imageList.size
    }

}